﻿#ifndef UTILS_H
#define UTILS_H

#include <QString>
#include "qaesencryption.h"
#include <QCryptographicHash>
#include <QDebug>
#include <QException>

class MyUtils : public QObject{
public:
    MyUtils();
    ~MyUtils();
    //加密字符串
    static QString getEncodStr(QString src,QString key){
        QString estr = "";
        try {
            QAESEncryption encryption(QAESEncryption::AES_128,QAESEncryption::ECB,QAESEncryption::ZERO);
            QByteArray haskey = QCryptographicHash::hash(QString(key).toUtf8(),QCryptographicHash::Md5);
            QByteArray encodeText = encryption.encode(src.toUtf8(),haskey);
            estr = QString::fromLatin1(encodeText.toBase64());
        } catch (QException e) {

        }

        return estr;
    }
    //解密字符串
    static QString getDecodStr(QString src,QString key)
    {
        QString dstr = "";
        try {
            QAESEncryption encryption(QAESEncryption::AES_128,QAESEncryption::ECB,QAESEncryption::ZERO);
            QByteArray haskey = QCryptographicHash::hash(QString(key).toUtf8(),QCryptographicHash::Md5);
            QByteArray dba = encryption.decode(QByteArray::fromBase64(src.toLatin1()),haskey);
            dstr = QString::fromUtf8(dba);
        } catch (QException e) {
            qDebug()<<"解密发生异常";
        }

        return dstr;
    }
};

#endif // UTILS_H
